{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1e199d3a-70ea-4f01-a662-41ef98b46608",
   "metadata": {},
   "source": [
    "<center><img src=\"./image/CLBLOGO.jpg\" alt=\"创乐博\" style=\"width: 300px;\"/></center>\r\n",
    "\n",
    "# 275旋转编码器实验验\n",
    "\n",
    "@----湖南创乐博智能科技有限公司---b<>r>@  文件:名75旋转编码器实验验.ipynb <br>\n",
    "@  版本：V2.0 <br>\n",
    "@  author: zhuli nb r\n",
    "   @说: 旋转bianmq 制rr>>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18eeae8d-41bf-4a01-9bfa-9905f97d7aca",
   "metadata": {},
   "source": [
    "## 1.导入必要的库文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6783bd1e-41be-4c92-b09b-c1db11e9fdec",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from gpiozero import RotaryEncoder, Button\n",
    "from time import sleep"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "993b83e8-b271-4420-9619-a216088a8f56",
   "metadata": {},
   "source": [
    "## 2.定义旋转编码器管脚"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e495a5d8-b906-402d-b6d2-198f0c76555c",
   "metadata": {},
   "outputs": [],
   "source": [
    "encoder = RotaryEncoder(a=17, b=18)  #旋转编码器的CLK连接GPIO 17，DT连接GPIO 18\n",
    "button = Button(27)                  # SW连接 GPIO27"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f5f58331-df2f-409c-b08b-1401a5100a80",
   "metadata": {},
   "source": [
    "## 3.初始化函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "24bb5df6-9701-49a1-8df4-dcbd1a87c5fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "makerobo_globalCounter = 0          # 计数器值\n",
    "makerobo_tmp = 0\t                # 当前状态判断"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf81ed90-ea51-4447-b522-85494d089b54",
   "metadata": {},
   "source": [
    "## 4.功能函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "20561d89-e334-41e0-9358-07f9b385ffdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 旋转编码方向位判断函数\n",
    "def makerobo_rotaryDeal():\n",
    "    global makerobo_globalCounter\n",
    "    global makerobo_tmp\n",
    "    makerobo_globalCounter += encoder.steps  # 根据编码器步骤调整计数器\n",
    "    encoder.steps = 0 # 更新计数器后重置编码器步骤\n",
    "    if makerobo_tmp != makerobo_globalCounter: # 判断状态值发生改变\n",
    "        print ('makerobo_globalCounter = %d' % makerobo_globalCounter) # 打印出状态信息\n",
    "        makerobo_tmp = makerobo_globalCounter    #  把当前状态赋值到下一个状态，避免重复打印\n",
    "\n",
    "# 中间按键按下响应程序\n",
    "def makerobo_btnISR():\n",
    "    global makerobo_globalCounter\n",
    "    makerobo_globalCounter = 0     # 复位计数器值\n",
    "    print('Counter reset') # 计数器值复位\n",
    "\n",
    "# 按键按下中断响应程序\n",
    "button.when_pressed = makerobo_btnISR"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b01200-b8f4-4906-9fef-ee5921375822",
   "metadata": {},
   "source": [
    "## 5.主程序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "45e18609-d482-4008-9b7f-64ee3599f522",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "makerobo_globalCounter = 1\n",
      "makerobo_globalCounter = 2\n",
      "makerobo_globalCounter = 4\n",
      "makerobo_globalCounter = 6\n",
      "makerobo_globalCounter = 7\n",
      "makerobo_globalCounter = 8\n",
      "makerobo_globalCounter = 11\n",
      "makerobo_globalCounter = 13\n",
      "makerobo_globalCounter = 14\n",
      "makerobo_globalCounter = 16\n",
      "makerobo_globalCounter = 18\n",
      "makerobo_globalCounter = 20\n",
      "makerobo_globalCounter = 21\n",
      "makerobo_globalCounter = 26\n",
      "makerobo_globalCounter = 28\n",
      "makerobo_globalCounter = 30\n",
      "makerobo_globalCounter = 35\n",
      "makerobo_globalCounter = 37\n",
      "Counter reset\n",
      "makerobo_globalCounter = 0\n",
      "makerobo_globalCounter = -5\n",
      "makerobo_globalCounter = -7\n",
      "makerobo_globalCounter = -14\n",
      "makerobo_globalCounter = -16\n"
     ]
    }
   ],
   "source": [
    "# 程序入口\n",
    "if __name__ == '__main__':\n",
    "    try:\n",
    "        while True:\n",
    "            makerobo_rotaryDeal()  # 更新编码器值\n",
    "            sleep(0.1)             # 短延迟，降低CPU负载\n",
    "    except KeyboardInterrupt:\n",
    "        pass"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
